/**
 * 格式化日期格式 (用于兼容ios Date对象)
 */
const formatDate = (time) => {
  // 将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式 
  return time.replace(/\-/g, "/");
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function randomArr(allCount) {
  var arr = [];
  for(var i = 0; i < 10; i++) {
    var n = Math.floor(Math.random() * allCount);
    arr[i] = n;
  }
  return arr;
}
function getRandomArrayElements(arr, count) {
  var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
  while (i-- > min) {
  index = Math.floor((i + 1) * Math.random());
  temp = shuffled[index];
  shuffled[index] = shuffled[i];
  shuffled[i] = temp;
  }
  return shuffled.slice(min);
  }

function showLoading(message) {
  if (wx.showLoading) {
    // 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
    wx.showLoading({
      title: message,
      mask: true
    });
  } else {
    // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
    wx.showToast({
      title: message,
      icon: 'loading',
      mask: true,
      duration: 20000
    });
  }
}

function hideLoading() {
  if (wx.hideLoading) {
    // 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
    wx.hideLoading();
  } else {
    wx.hideToast();
  }
}

function toarray(value) {      
  return (value || "").split(",")
}

/**
 * 获取二维码场景值(scene)
 */
export const getSceneData = (query) => {
  return query.scene ? sceneDecode(query.scene) : {}
}

module.exports = {
	formatDate: formatDate,
	formatTime: formatTime,
	randomArr:randomArr,
	getRandomArrayElements:getRandomArrayElements,
	showLoading: showLoading,
	hideLoading: hideLoading,
	toarray: toarray,
}
